package com.xjcs.saplingplan.view

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.graphics.Typeface
import android.text.TextUtils
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.res.ResourcesCompat
import com.xjcs.saplingplan.R

class CustomTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) {
    // Getter 方法
    // 属性
    private var bgColor: Int = 0
    private var strokeColor = 0
    private var cornerRadius = 0f
    private var strokeWidth = 0f

    // 单独的圆角半径
    private var topLeftRadius = 0f
    private var topRightRadius = 0f
    private var bottomLeftRadius = 0f
    private var bottomRightRadius = 0f

    // 绘制相关
    private var backgroundPaint: Paint? = null
    private var borderPaint: Paint? = null
    private var rectF: RectF? = null
    private var path: Path? = null

    // 字体相关
    private var regularTypeface: Typeface? = null
    private var boldTypeface: Typeface? = null

    init {
        init(context, attrs, defStyleAttr)
    }

    private fun init(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
        // 从XML属性中获取值
        val typedArray: TypedArray = context.obtainStyledAttributes(
            attrs,
            R.styleable.CustomTextView,
            defStyleAttr,
            0
        )

        try {
            bgColor =
                typedArray.getColor(R.styleable.CustomTextView_bgColor, Color.TRANSPARENT)
            strokeWidth = typedArray.getDimension(R.styleable.CustomTextView_strokeWidth, 0f)
            strokeColor =
                typedArray.getColor(R.styleable.CustomTextView_strokeColor, Color.TRANSPARENT)
            // 统一圆角
            cornerRadius = typedArray.getDimension(R.styleable.CustomTextView_cornerRadius, 0f)
            // 单独圆角
            topLeftRadius =
                typedArray.getDimension(R.styleable.CustomTextView_topLeftRadius, cornerRadius)
            topRightRadius =
                typedArray.getDimension(R.styleable.CustomTextView_topRightRadius, cornerRadius)
            bottomLeftRadius =
                typedArray.getDimension(R.styleable.CustomTextView_bottomLeftRadius, cornerRadius)
            bottomRightRadius =
                typedArray.getDimension(R.styleable.CustomTextView_bottomRightRadius, cornerRadius)
        } finally {
            typedArray.recycle()
        }

        // 初始化字体
        try {
            regularTypeface = ResourcesCompat.getFont(context, R.font.base_regular)
            boldTypeface = ResourcesCompat.getFont(context, R.font.base_bold)
        } catch (e: Exception) {
            // 如果加载失败，使用默认字体
            regularTypeface = Typeface.DEFAULT
            boldTypeface = Typeface.DEFAULT_BOLD
        }
        includeFontPadding = false

        // 初始化画笔
        initPaints()
    }

    private fun initPaints() {
        // 背景画笔
        backgroundPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        backgroundPaint?.style = Paint.Style.FILL
        backgroundPaint?.color = bgColor

        // 边框画笔
        borderPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        borderPaint?.style = Paint.Style.STROKE
        borderPaint?.color = strokeColor
        borderPaint?.strokeWidth = strokeWidth
        borderPaint?.strokeCap = Paint.Cap.ROUND
        borderPaint?.strokeJoin = Paint.Join.ROUND

        rectF = RectF()
        path = Path()
    }

    override fun onDraw(canvas: Canvas) {
        // 绘制背景和边框
        drawBackgroundAndBorder(canvas)

        // 绘制文字（调用父类方法）
        super.onDraw(canvas)
    }

    private fun drawBackgroundAndBorder(canvas: Canvas) {
        val halfStroke = strokeWidth / 2
        rectF?.set(halfStroke, halfStroke, width - halfStroke, height - halfStroke)

        path?.reset()
        path?.addRoundRect(
            rectF!!,
            floatArrayOf(
                topLeftRadius, topLeftRadius,
                topRightRadius, topRightRadius,
                bottomRightRadius, bottomRightRadius,
                bottomLeftRadius, bottomLeftRadius
            ),
            Path.Direction.CW
        )

        // 先绘制背景
        backgroundPaint?.let { canvas.drawPath(path!!, it) }

        // 再绘制边框
        if (strokeWidth > 0) {
            borderPaint?.let { canvas.drawPath(path!!, it) }
        }
    }

    // Setter 方法
    fun setBgColor(backgroundColor: Int) {
        this.bgColor = backgroundColor
        backgroundPaint?.color = bgColor
        invalidate()
    }

    fun setBorderColor(borderColor: Int) {
        this.strokeColor = borderColor
        borderPaint?.color = borderColor
        invalidate()
    }

    fun setBorderWidth(borderWidth: Float) {
        this.strokeWidth = borderWidth
        borderPaint?.strokeWidth = borderWidth
        invalidate()
    }

    fun setCornerRadius(cornerRadius: Float) {
        this.cornerRadius = cornerRadius
        this.topLeftRadius = cornerRadius
        this.topRightRadius = cornerRadius
        this.bottomLeftRadius = cornerRadius
        this.bottomRightRadius = cornerRadius
        invalidate()
    }

    fun setCornerRadius(topLeftRadius: Float, topRightRadius: Float, bottomLeftRadius: Float, bottomRightRadius: Float) {
        this.topLeftRadius = topLeftRadius
        this.topRightRadius = cornerRadius
        this.bottomLeftRadius = cornerRadius
        this.bottomRightRadius = cornerRadius
        invalidate()
    }

    fun setTopLeftRadius(topLeftRadius: Float) {
        this.topLeftRadius = topLeftRadius
        invalidate()
    }

    fun setTopRightRadius(topRightRadius: Float) {
        this.topRightRadius = topRightRadius
        invalidate()
    }

    fun setBottomLeftRadius(bottomLeftRadius: Float) {
        this.bottomLeftRadius = bottomLeftRadius
        invalidate()
    }

    fun setBottomRightRadius(bottomRightRadius: Float) {
        this.bottomRightRadius = bottomRightRadius
        invalidate()
    }

    fun getBorderColor(): Int {
        return strokeColor
    }

    fun getBorderWidth(): Float {
        return strokeWidth
    }

    fun getCornerRadius(): Float {
        return cornerRadius
    }

    fun getTopLeftRadius(): Float {
        return topLeftRadius
    }

    fun getTopRightRadius(): Float {
        return topRightRadius
    }

    fun getBottomLeftRadius(): Float {
        return bottomLeftRadius
    }

    fun getBottomRightRadius(): Float {
        return bottomRightRadius
    }
}